package com.ruoyi.redtea.dualprevent.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.redtea.dualprevent.domain.bo.TrainAnalyseUnitBo;
import com.ruoyi.redtea.dualprevent.domain.entity.TrainAnalyseUnit;
import com.ruoyi.redtea.dualprevent.domain.entity.TrainAssessAnalyseUnit;
import com.ruoyi.redtea.dualprevent.domain.entity.TrainRegionAnalyseunit;
import com.ruoyi.redtea.dualprevent.mapper.TrainAssessAnalyseUnitMapper;
import com.ruoyi.redtea.dualprevent.mapper.TrainRegionAnalyseunitMapper;
import com.ruoyi.redtea.dualprevent.service.TrainAnalyseUnitService;
import com.ruoyi.redtea.dualprevent.mapper.TrainAnalyseUnitMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lgp
 * @description 针对表【train_analyse_unit(分析单元)】的数据库操作Service实现
 * @createDate 2024-05-14 15:40:14
 */
@Service
public class TrainAnalyseUnitServiceImpl implements TrainAnalyseUnitService {

    /**
     * 分析单元Mapper
     */
    @Resource
    private TrainAnalyseUnitMapper trainAnalyseUnitMapper;
    /**
     * 风险辨识Mapper
     */
    @Resource
    private TrainAssessAnalyseUnitMapper trainAssessAnalyseUnitMapper;
    /**
     * 分析单元关联区域
     */
    @Resource
    private TrainRegionAnalyseunitMapper trainRegionAnalyseunitMapper;

    @Override
    public TrainAnalyseUnit queryById(Long id) {
        return trainAnalyseUnitMapper.selectVoById(id);
    }

    @Override
    public boolean insert(List<TrainAnalyseUnitBo> trainAnalyseUnitBos) {
        //三表插入：插入时需要添加分析单元库、风险辨识库、区域关联库 3库
        //分析单元列表
        List<TrainAnalyseUnit> idsUnits = new ArrayList<>();
        for (TrainAnalyseUnitBo trainAnalyseUnitBo : trainAnalyseUnitBos) {
            TrainAnalyseUnit trainAssessAnalyseUnit = BeanUtil.toBean(trainAnalyseUnitBo, TrainAnalyseUnit.class);
            idsUnits.add(trainAssessAnalyseUnit);
        }
        trainAnalyseUnitMapper.setListDelFlag(idsUnits);
        boolean anaunit = trainAnalyseUnitMapper.insertBatch(idsUnits);

        boolean assess = false;
        boolean unionRegion = false;
        if (anaunit) {
            //风险辨识列表
            List<TrainAssessAnalyseUnit> idsAssess = new ArrayList<>();
            //关联区域列表
            List<TrainRegionAnalyseunit> idsWithRegions = new ArrayList<>();

            for (int i = 0; i < trainAnalyseUnitBos.size(); i++) {
                TrainAnalyseUnitBo analyseUnitBo = trainAnalyseUnitBos.get(i);
                analyseUnitBo.setId(idsUnits.get(i).getId());

                TrainAssessAnalyseUnit assessAnalyseUnit = new TrainAssessAnalyseUnit();
                assessAnalyseUnit.setAnaunitId(analyseUnitBo.getId());
                idsAssess.add(assessAnalyseUnit);

                TrainRegionAnalyseunit idsWithRegion = new TrainRegionAnalyseunit();
                idsWithRegion.setRegionId(analyseUnitBo.getRegionId());
                idsWithRegion.setAnalyseUnitId(analyseUnitBo.getId());
                idsWithRegions.add(idsWithRegion);
            }
            //将分析单元id记录插入到辨识记录里
            trainAssessAnalyseUnitMapper.setListDelFlag(idsAssess);
            assess = trainAssessAnalyseUnitMapper.insertBatch(idsAssess);
            //插入到区域关联记录中
            trainRegionAnalyseunitMapper.setListDelFlag(idsWithRegions);
            unionRegion = trainRegionAnalyseunitMapper.insertBatch(idsWithRegions);
        }
        if (anaunit && assess && unionRegion) {
            return true;
        } else {
            throw new ServiceException("分析单元添加失败！");
        }
    }

    @Override
    public boolean update(TrainAnalyseUnit trainAnalyseUnit) {
        return trainAnalyseUnitMapper.updateById(trainAnalyseUnit) == 1;
    }

    @Override
    public boolean deleteByIds(List<Long> trainAnalyseUnits) {
        return trainAnalyseUnitMapper.deleteBatchIds(trainAnalyseUnits) == 1;
    }
}




